package org.darcy.youchat.activity.message;

import android.view.MenuItem;
import android.view.View;

import com.netease.nimlib.sdk.RequestCallback;

import org.darcy.youchat.R;
import org.darcy.youchat.activity.BaseActivity;
import org.darcy.youchat.databinding.ActivityPostscriptBinding;
import org.darcy.youchat.nimsdk.NimFriendSDK;
import org.darcy.youchat.utils.UIUtils;

/**
 * 附言
 */
public class PostscriptActivity extends BaseActivity {

    private ActivityPostscriptBinding binding;
    private final String titleName = "附言";

    public String mAccount;//账号
    public String mMsg;//附言


    @Override
    public void init() {
        mAccount = getIntent().getStringExtra("account");
    }

    @Override
    public void initView() {
        binding = ActivityPostscriptBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setSupportActionBar(binding.mToolbar.toolbar);
        getSupportActionBar().setTitle(UIUtils.getString(R.string.app_name));
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        binding.mToolbar.btnOk.setVisibility(View.VISIBLE);
        binding.mToolbar.btnOk.setText("发送");
    }

    /**
     * Listener初始化
     */
    public void initListener() {
        binding.mToolbar.btnOk.setOnClickListener(v -> {
            showWaitingDialog("请稍等");
            mMsg = binding.etMsg.getText().toString();
            //发送添加好友请求
            NimFriendSDK.addFriend(mAccount, mMsg, new RequestCallback<Void>() {
                @Override
                public void onSuccess(Void param) {
                    hideWaitingDialog();
                    UIUtils.showToast("添加好友申请成功");
                    finish();
                }

                @Override
                public void onFailed(int code) {
                    UIUtils.showToast("添加好友失败" + code);
                    hideWaitingDialog();
                }

                @Override
                public void onException(Throwable exception) {
                    exception.printStackTrace();
                    hideWaitingDialog();
                }
            });
        });

        //清空
        binding.ibClear.setOnClickListener(view -> binding.etMsg.setText(""));
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }


}
